public class InsertSort {
    public static void main(String[] args) {
        Integer[] array = {9, 6, 3, 2, 5, 5, 1, 7, 8, 2, 4, 3};
        //
        System.out.println("\ninsertSortRes:");
        Integer[] insertSortRes = insertSort(array);
        for (int i = 0; i < insertSortRes.length; i++) {
            System.out.printf(insertSortRes[i] + ",");
        }

    }

    // insertSort-01
    public static Integer[] insertSort(Integer[] array) {
        for (int i = 1; i < array.length; i++) {
            Integer currentItem = array[i];
            // 移动
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > currentItem) {
                    array[j + 1] = array[j];
                } else {
                    break; // 注意此处
                }
            }
            // 插入
            array[j + 1] = currentItem;
        }
        return array;
    }

}
